package com.ttloner3.connectionpool;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author TTLoner
 * @date 2021/6/25 15:44
 * @description
 * 阿帕奇的C3P0数据库连接池
 */
public class C3P0Test {
    @Test
    public void testGetConnection() throws PropertyVetoException, SQLException, IOException {
        //获取c3p0数据库连接池
        /*
         * @author: TTLoner
         * @time: 2021/6/25 15:57
         * @description:
         *原始硬编码方式：
         */
//        ComboPooledDataSource cpds = new ComboPooledDataSource();
//        cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver
//        cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/test" );
//        cpds.setUser("root");
//        cpds.setPassword("root");

        //通过properties方式
//        Properties properties = new Properties();
//        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
//        properties.load(is);
//        String user = properties.getProperty("user");
//        String driverclass = properties.getProperty("driverclass");
//        String password = properties.getProperty("password");
//        String url = properties.getProperty("url");
//        ComboPooledDataSource cpds = new ComboPooledDataSource();
//        cpds.setDriverClass(driverclass);
//        cpds.setUser(user);
//        cpds.setPassword(password);
//        cpds.setJdbcUrl(url);

        //通过xml文件
        ComboPooledDataSource cpds = new ComboPooledDataSource("helloc3p0");
        //设置初始时数据库连接池中的连接数
        Connection conn = cpds.getConnection();
        System.out.println(conn);
    }
}
